<template>
  <div>
    <MyTable :list="list">
      <!-- 封装 -->
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <!-- 作用域插槽传值 -->
      <template #body="scope">
        <td>{{ scope.row.id }}</td>
        <td>{{ scope.row.goods_name }}</td>
        <td>{{ scope.row.goods_price }}</td>
        <td>
          <!-- 输入框 -->
          <input
            type="text"
            class="tag-input form-control"
            style="width: 100px"
            v-if="scope.row.inputVisible"
            v-fff
            @blur="scope.row.inputVisible = false"
            v-model="scope.row.inputValue"
            @keydown.esc="scope.row.inputValue = ''"
            @keydown.enter="
              scope.row.inputValue.trim() === ''
                ? scope.row.inputValue.trim()
                : scope.row.tags.push(scope.row.inputValue);
              scope.row.inputValue = '';
            "
          />
          <!-- 点击按钮 -->
          <button
            v-else
            style="display: block"
            class="btn btn-primary btn-sm add-tag"
            @click="scope.row.inputVisible = true"
          >
            +tags
          </button>
          <!-- 循环标签 -->
          <span
            v-for="(item, index) in scope.row.tags"
            :key="index"
            class="badge badge-warning"
            >{{ item }}</span
          >
        </td>
        <td>
          <button
            @click="list = list.filter((item) => item.id !== scope.row.id)"
          >
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable";

export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    };
  },
  async created() {
    const res = await this.axios({
      url: "/api/goods",
    }).catch((err) => {
      console.log(err);
    });
    this.list = res.data.data;
    console.log(this.list);
  },
  directives: {
    fff: {
      inserted(el) {
        el.focus();
      },
    },
  },
};
</script>